package cn.lingyangwl.agile.dynamic.client;

import cn.lingyangwl.agile.dynamic.config.core.event.ConfigDeleteEvent;
import cn.lingyangwl.agile.model.enums.ConfigDefineEnum;
import cn.lingyangwl.agile.model.module.tenant.TenantContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @author shenguangyang
 */
@Slf4j
@Component
public class ClientConfigDeleteListener {

    @EventListener(ConfigDeleteEvent.class)
    public void defaultDelete(ConfigDeleteEvent event) {
        Object configKey = event.getSource();
        ConfigDefineEnum configDefine = ConfigDefineEnum.ofByKey((String) configKey);
        AbstractClientProcess clientProcess = AbstractClientProcess.getClientProcess(configDefine);
        if (Objects.isNull(clientProcess)) {
            return;
        }
        clientProcess.delete(TenantContext.get().getTenantId());
        log.info("configKey [{}] 被删除", configKey);
    }
}
